#-------------------------------------------------------------------------------
# Name:        funs_ext
# Purpose:
#
# Author:     SUPER_紫电  QQ,911344755
#
# Created:    21/02/2023
# Copyright:  (c) SUPER_紫电 2022 All rights reserved.
# Licence:    <Modified BSD License>
#-------------------------------------------------------------------------------

# Internal modules:
#import actions, ata, colorconsole, memorymap, mutex, pci, wdfile
#import common, errcode, familyid, ioeng, taskfile, vsc, vsctype
#
from trex import *

def flashr(start_offset, bytes_cnt):
    action_code = AC_RWERASEFLASH
    func_code = FC_Flash.eRdFlash.value
    param_list = [
        func_code,
        start_offset & 0xFFFF,
        start_offset >> 16,
        bytes_cnt & 0xFFFF,
        bytes_cnt >> 16
    ]
    return vsc(action_code, param_list)

def read_flash():
    print('\n[Read Flash]')
    if flashr(0, 0) != 0:
        return False
    blk = get_sectors()
    buffer = bytearray(blk*512)
    if not smart_read_data(buffer, 0, blk):
        return False
    if not rstat_bitclr(0):
        return False
    filename = common.workdir + '\\ROM.bin'
    return save_file(filename, buffer)

def poll_vscstat():
    if not get_vsc_status():
        print('Error executing command')
        return
    drv_state1 = {
        0: 'Idle',
        1: 'Standby',
        2: 'Sleep',
        3: 'Background DST',
        4: 'Background Data Lifeguard',
        5: 'Background VSC Command',
        6: 'PST test in progress',
    }
    vscstatus = vsctype.vsc_status
    cur_state = vscstatus.CurState
    result = '\n\nCurrent Drive State 1 = %d %s' % (cur_state, drv_state1[cur_state])
    if cur_state != 6:
        print(result)
        return
    print(result, end='\r')
    start = time()
    while cur_state == 6:
        result = '\n\nCurrent Drive State 1 = %d %s' % (cur_state, drv_state1[cur_state])
        result += '\n------------------------------- -----------------------------'
        result += '\nPTM File Id         = 0x%X' % vscstatus.PTMId
        result += '\nPTM Test Id         = 0x%X' % vscstatus.TestId
        result += '\nCurrent VSC Command = 0x%X' % vscstatus.ActionCode
        result += '\nExtended Error Code = 0x%X' % vscstatus.ExtErr
        if not get_vsc_status(): break
        vscstatus = vsctype.vsc_status
        cur_state = vscstatus.CurState
        ptm_id = vscstatus.PTMId
        print(result, end='\r')
        if ptm_id == 0xFF22 or ptm_id == 0xFFF1:
            break

def mini_test(file_path, file_id, test_id):
    if not wbx_pst(file_path, file_id, 0):
        return False
    if xpst(file_id, test_id) != 0:
        return False
    return rstat_bitclr(0)

def mini_cal(file_path):
    if mini_test(file_path, 0xB1, 0x4E0):
        poll_vscstat()

def seam_cal(file_path):
    if mini_test(file_path, 0xB1, 0x160):
        poll_vscstat()

def mini_wrro(file_path, test_id=0):
    if mini_test(file_path, 0xB2, test_id):
        poll_vscstat()

def mini_wrro2(file_path):
    if mini_wrro(file_path, 2):
        poll_vscstat()

def mini_ibi(file_path):
    return mini_test(file_path, 0xDF, 1)

def mini_arco(file_path , head_dcm, media_dcm):
    file_id = 0x2401 if os.path.exists(file_path + '\\2401.rpm') else 0xC4
    if not wbx_pst(file_path, file_id, 0):
        return False
    filename = file_path + '\\C5.rpm'
    if not ldbin(filename):
        return False
    b = vsctype.super_buffer[0x24]
    if b == 0: start_offset = 256
    if b == 2: start_offset = 512
    if b == 3: start_offset = 640
    if b == 4:
        b == vsctype.super_buffer[0x23]
        if b == 0x00: start_offset = 0x300
        if b == 0x50: start_offset = 808
        if b == 0xD0: start_offset = 872
        if b == 0xE0: start_offset = 880
        if b == 0xF0: start_offset = 888
        if b == 0xF8: start_offset = 892
    if b == 5:
        b == vsctype.super_buffer[0x23]
        if b == 0x84: start_offset = 962
        if b == 0xC0: start_offset = 992
        if b == 0xE0: start_offset = 1008
    if not wbx_pst(file_path, 0xC5, start_offset):
        return False
    if xpst(file_id, 1, [0x44, 0x19, head_dcm, media_dcm]) != 0:
        return False
    return rstat_bitclr(0)

def mini_arco_test(file_path):
    head_dcm, media_dcm = 0, 0
    if rdfl(0xA):
        hdr_size  = vsctype.super_buffer[6]
        head_dcm  = vsctype.super_buffer[hdr_size+13]
        media_dcm = vsctype.super_buffer[hdr_size+12]
    if head_dcm == 0 or media_dcm == 0:
        if not ldbin(file_path + '\\28.rpm'):
            return
        head_dcm  = vsctype.super_buffer[0x44]
        media_dcm = vsctype.super_buffer[0x48]
    if mini_arco(file_path, head_dcm, media_dcm):
        poll_vscstat()

